package cn.xfdzx.android.apps.shop.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.ForgetPasswordActivity;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.evenbus.CarInitDataMessage;
import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.util.CleanMessageUtil;
import cn.xfdzx.android.apps.shop.util.DateClearManager;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/*
/* 设置页面
 */
public class SettingActivity extends BaseMvpActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;      //标题栏-左边按钮
    @BindView(R.id.tvcatch)
    TextView tvcatch;    //清除缓存
    @BindView(R.id.rl_aboutour)
    RelativeLayout rl_aboutour;  //关于我们
    @BindView(R.id.tvLogout)
    TextView tvLogout;   //退出登录
    private Dialog dialog;//确认清除弹窗


//    @BindView(R.id.password_info)
//    TextView passwordInfo;    //红色字体提示文案
//    @BindView(R.id.password_agreement)
//    TextView passwordAgreement;  //用户协议
//    @BindView(R.id.password_privacy)
//    TextView passwordPrivacy;   //隐私权限


    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        getCathSize();//获取缓存大小

        dialog = Utils.addDialog(this, R.layout.dialog, R.string.confirmclearcatch, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清除操作
                DateClearManager.clearAllCache(SettingActivity.this);
                getCathSize();//获取缓存大小
                dialog.dismiss();
            }
        });
        if ( TYPE_IS_RETAIL == 0) {
            tvLogout.setBackgroundResource(R.drawable.dfb9129_bg_r19);
        } else {
            tvLogout.setBackgroundResource(R.drawable.d02c557_bg_r19);
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @OnClick({R.id.iv_back, R.id.tvcatch, R.id.rl_aboutour, R.id.tvLogout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tvcatch:   //清除缓存点击
                dialog.show();
                break;
            case R.id.rl_aboutour: //关于我们点击
                Intent intent = new Intent(this, AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.tvLogout:   //退出登录点击
                showGoSetTimeDialog();

                break;
        }
    }

    private Dialog logoutDialog;//退出登录弹窗

    /**
     * 去设置弹窗
     */
    public void showGoSetTimeDialog() {
        logoutDialog = Utils.addDialog(this, R.layout.dialog, R.string.logoutsure, R.string.logout, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UtilsData.getPreference(SettingActivity.this, "login").remove("head_url");
                UtilsData.getPreference(SettingActivity.this, "login").remove("nickname");
                UtilsData.getPreference(SettingActivity.this, "login").remove("is_store");
                UtilsData.getPreference(SettingActivity.this, "login").remove("token");
                APP_TOKEN = "";

                logoutDialog.dismiss();
                finish();
            }
        });
        logoutDialog.show();
    }


    /**
     * 获取缓存大小
     */
    private void getCathSize() {
        try {
            //String totalCacheSize = DateClearManager.getTotalCacheSize(SettingCommonActivity.this);
            String totalCacheSize = CleanMessageUtil.getTotalCacheSize(getApplicationContext());
            Log.e("totalCacheSize", totalCacheSize + "==");
            tvcatch.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        if (dialog.isShowing()) {
            dialog.dismiss();

        }
        super.onDestroy();
    }

}
